﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Spore.Validator
{
    /// <summary>
    /// 最大值验证器，验证字段小于或等于最大值
    /// </summary>
   public class ValidatorMaxValue:ValidatorBase
    {
        private double maxValue;
        /// <summary>       
        /// </summary>        
        /// <param name="errMsg">验证不通过时的错误信息</param>
        public ValidatorMaxValue(double maxValue, string errMsg)
        {
            this.maxValue = maxValue;
            this.ErrorMessage = errMsg;
        }
        public ValidatorMaxValue(int maxValue)
        {
            this.maxValue = maxValue;
            this.ErrorMessage = "不能大于"+maxValue.ToString();
        }

        /// <summary>
        ///验证内容是否大于指定值
        /// </summary>
        /// <param name="controlToValidate">要验证的控件</param>
        /// <returns></returns>
        public override bool IsValid(Control controlToValidate)
        {
            string content = controlToValidate.Text;
            double tmp;
           if( double.TryParse(content, out tmp))
            return tmp <= maxValue;
           return false;
        }
    }
}
